package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.PlatMchntInfoMapper;
import cc.rengu.igas.bomp.common.entity.PlatMchntInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台商户正式表接口实现类
 * Created by 赵奎伍 on 2020/3/23
 */
public class PlatMchntInfoMapperImpl implements PlatMchntInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<PlatMchntInfo> selectPlatMchntInfoByInstId(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPlatMchntInfoByInstId");
        PlatMchntInfo platMchntInfo = new PlatMchntInfo();
        platMchntInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, platMchntInfo, PlatMchntInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取平台商户信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (PlatMchntInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectPlatMchntInfoByInstIdByPage(String instId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPlatMchntInfoByInstId");
        PlatMchntInfo platMchntInfo = new PlatMchntInfo();
        platMchntInfo.setInstId(instId);
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, platMchntInfo, PlatMchntInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("获取平台商户信息失败,instId:<{}>", instId);
            return null;
        }
        return pageResult;
    }

    @Override
    public List<PlatMchntInfo> selectPlatMchntInfoByPlatMchntNoOrPlatMchntNameOrSignInstId(String instId, String platMchntNo, String platMchntName, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPlatMchntInfoByPlatMchntNoOrPlatMchntNameOrSignInstId");
        PlatMchntInfo platMchntInfo = new PlatMchntInfo();
        platMchntInfo.setInstId(instId);
        if (null != platMchntNo && !platMchntNo.isEmpty()) {
            platMchntInfo.setPlatMchntNo(platMchntNo);
        }
        if (null != platMchntName && !platMchntName.isEmpty()) {
            platMchntInfo.setPlatMchntName(platMchntName);
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            platMchntInfo.setManageInstId(signInstId);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, platMchntInfo, PlatMchntInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据平台商户号查询或平台商户名称模糊查询或根据签约机构代码查询信息失败,instId:<{}>,platMchntNo:<{}>,platMchntName:<{}>,signInstId:<{}>", instId, platMchntNo, platMchntName, signInstId);
            return null;
        }
        return objectList.stream().map(item -> (PlatMchntInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectPlatMchntInfoByPlatMchntNoOrPlatMchntNameOrSignInstIdByPage(String instId, String platMchntNo, String platMchntName, String signInstId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPlatMchntInfoByPlatMchntNoOrPlatMchntNameOrSignInstId");
        PlatMchntInfo platMchntInfo = new PlatMchntInfo();
        platMchntInfo.setInstId(instId);
        if (null != platMchntNo && !platMchntNo.isEmpty()) {
            platMchntInfo.setPlatMchntNo(platMchntNo);
        }
        if (null != platMchntName && !platMchntName.isEmpty()) {
            platMchntInfo.setPlatMchntName(platMchntName);
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            platMchntInfo.setManageInstId(signInstId);
        }
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, platMchntInfo, PlatMchntInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据平台商户号查询或平台商户名称模糊查询或根据签约机构代码查询信息失败,instId:<{}>,platMchntNo:<{}>,platMchntName:<{}>,signInstId:<{}>", instId, platMchntNo, platMchntName, signInstId);
            return null;
        }
        return pageResult;
    }

    @Override
    public PlatMchntInfo selectPlatMchntInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPlatMchntInfoByMchntNo");
        PlatMchntInfo platMchntInfo = new PlatMchntInfo();
        platMchntInfo.setInstId(instId);
        platMchntInfo.setPlatMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, platMchntInfo, platMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("根据平台商户号查询信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return platMchntInfo;
    }

    @Override
    public List<PlatMchntInfo> selectPlatMchntInfo(String instId, String platMchntNo, String platMchntName, String manageInstId, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompSelectNormalPlatMchntInfoByManageInstIds");
        PlatMchntInfo platMchntInfo = new PlatMchntInfo();
        platMchntInfo.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(signInstId)) {
            platMchntInfo.setRemark1(signInstId);
        }
        if (!StringUtil.isEmptyOrNull(platMchntNo)) {
            platMchntInfo.setPlatMchntNo(platMchntNo);
        }
        if (!StringUtil.isEmptyOrNull(platMchntName)) {
            platMchntInfo.setPlatMchntName(platMchntName);
        }
        if (!StringUtil.isEmptyOrNull(manageInstId)) {
            platMchntInfo.setManageInstId(manageInstId);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, platMchntInfo, PlatMchntInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据根据签约机构代码查询信息失败,instId:<{}>,管理机构列表:<{}>", instId, manageInstId);
            return null;
        }
        return objectList.stream().map(item -> (PlatMchntInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectPlatMchntInfoByPage(String instId, String platMchntNo, String platMchntName, String manageInstId, String signInstId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompSelectNormalPlatMchntInfoByManageInstIds");
        PlatMchntInfo platMchntInfo = new PlatMchntInfo();
        platMchntInfo.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(signInstId)) {
            platMchntInfo.setRemark1(signInstId);
        }
        if (!StringUtil.isEmptyOrNull(platMchntNo)) {
            platMchntInfo.setPlatMchntNo(platMchntNo);
        }
        if (!StringUtil.isEmptyOrNull(platMchntName)) {
            platMchntInfo.setPlatMchntName(platMchntName);
        }
        if (!StringUtil.isEmptyOrNull(manageInstId)) {
            platMchntInfo.setManageInstId(manageInstId);
        }
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, platMchntInfo, PlatMchntInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据根据签约机构代码查询信息失败,instId:<{}>,管理机构列表:<{}>", instId, signInstId);
            return null;
        }
        return pageResult;
    }
}
